package com.louis.iread.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.louis.iread.bean.NovelPageItem;
import com.louis.iread.skin.SkinMgr;

import java.util.List;

/**
 * Created by louis on 18-2-26.
 */

public class ClassPageAdapter extends BaseAdapter {
    private List<NovelPageItem> mItemList;
    private Context mContext;
    private LayoutInflater inflater;
    private int layoutId;
    private int variableId;
    public ClassPageAdapter(Context context, int layoutId, int resId, List<NovelPageItem> list) {
        mContext = context;
        mItemList = list;
        inflater = LayoutInflater.from(mContext);
        this.layoutId = layoutId;
        this.variableId = resId;
    }

    public void addItems(List<NovelPageItem> items) {
        mItemList.addAll(items);
    }

    @Override
    public int getCount() {
        return mItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return mItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewDataBinding dataBinding = null;
        if (view == null) {
            dataBinding = DataBindingUtil.inflate(inflater, layoutId, viewGroup, false);
            view = dataBinding.getRoot();
            view.setTag(dataBinding);
        }else{
            dataBinding = (ViewDataBinding) view.getTag();
        }
        dataBinding.setVariable(variableId, mItemList.get(position));
        SkinMgr.getInstance().applyView(view);
        return view;
    }
}
